package org.sojex.photo.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.sojex.photo.manage.SdcardManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

public class Util {
	public static final int DOWNSUCCESS = 1;
	public static final int DOWNFAIL = 501;
	public static final int INITSUCCESS = 2;
	public static final int INITFAIL = 502;
	public static void DownImage(final Context context, final String url,
			final String netPath,final String name,final Handler handler) {
		new Thread() {
			public void run() {
				try {
					System.out.println("in DownLoad Manage");
					SdcardManager.Download(context, url, netPath);
					GlobalData.ImageUri.remove(name);
					GlobalData.ImageUri.put(name, netPath);
					Message msg = new Message();
					msg.what = 1;
					handler.sendMessage(msg);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	public static Drawable loadImageFromUrl(String url) {
		URL m;
		InputStream i = null;
		try {
			m = new URL(url);
			i = (InputStream) m.getContent();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Drawable d = Drawable.createFromStream(i, "src");
		return d;
	}
	public static Bitmap loadBitmapFromUrl(String url) {
		URL m;
		InputStream i = null;
		try {
			m = new URL(url);
			i = (InputStream) m.getContent();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Bitmap b = BitmapFactory.decodeStream(i);
		return b;
	}
	public static boolean isExist(String filePath){
		File file = new File(filePath);
		if(file.exists())
			return true;
		else
			return false;
	}
}
